export function createPad(params) {
	// 一则运算
	const calcNotZero = params.difficultTypes.includes("计算值不为0")
	const calcNotOne = params.difficultTypes.includes("计算值不为1")
	const resultNotZero = params.difficultTypes.includes("得数不为0")
	const resultNotOne = params.difficultTypes.includes("得数不为1")
	const arr = []
	if(params.name ==="九九乘法表20道"){
		while(arr.length<params.calcTotalCount){
			let obj = {
				firstNum: getCalc(true, false, params.firstNum-1),
				secondNum: getCalc(true, false, params.secondNum-1),
				calcType:"*"
			};
			arr.push(obj)
		}
		return arr
	}
	while (arr.length < params.calcTotalCount) {
		let obj = {
			firstNum: getCalc(calcNotZero, calcNotOne, params.firstNum),
			secondNum: getCalc(calcNotZero, calcNotOne, params.secondNum),
		};
		if(resultNotZero && resultNotOne){
			if(params.selectCalcTypes.includes("+") && obj.firstNum+obj.secondNum<params.resultNum){
				if(obj.firstNum+obj.secondNum!=0 && obj.firstNum+obj.secondNum!=1){
					obj.calcType="+"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("-")){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				
				if(obj.firstNum - obj.secondNum<params.resultNum){
					if(obj.firstNum-obj.secondNum!=0 && obj.firstNum-obj.secondNum!=1){
						obj.calcType="-"
						arr.push(obj)
						continue
					}
				}
			}
			if(params.selectCalcTypes.includes("*") && obj.firstNum*obj.secondNum<params.resultNum){
				if(obj.firstNum*obj.secondNum!=0 && obj.firstNum*obj.secondNum!=1){
					obj.calcType="*"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("/")){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				if(obj.firstNum / obj.secondNum<params.resultNum){
					if(obj.firstNum/obj.secondNum!=0 && obj.firstNum/obj.secondNum!=1){
						obj.calcType="/"
						arr.push(obj)
						continue
					}
				}
			}
		}else if(resultNotZero && !resultNotOne){
			if(params.selectCalcTypes.includes("+") && obj.firstNum+obj.secondNum<params.resultNum){
				if(obj.firstNum+obj.secondNum!=0){
					obj.calcType="+"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("-")){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				if(obj.firstNum - obj.secondNum <params.resultNum){
					if(obj.firstNum-obj.secondNum!=0){
						obj.calcType="-"
						arr.push(obj)
						continue
					}
				}
			}
			if(params.selectCalcTypes.includes("*") && obj.firstNum*obj.secondNum<params.resultNum){
				if(obj.firstNum*obj.secondNum!=0){
					obj.calcType="*"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("/")){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				if(obj.firstNum / obj.secondNum <params.resultNum){					
					if(obj.firstNum/obj.secondNum!=0){
						obj.calcType="/"
						arr.push(obj)
						continue
					}
				}
			}
		}else if(!resultNotZero && resultNotOne){
			if(params.selectCalcTypes.includes("+") && obj.firstNum+obj.secondNum<params.resultNum){
				if(obj.firstNum+obj.secondNum!=1){
					obj.calcType="+"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("-") && obj.firstNum-obj.secondNum<params.resultNum){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				if(obj.firstNum - obj.secondNum <params.resultNum){					
					if(obj.firstNum-obj.secondNum!=1){
						obj.calcType="-"
						arr.push(obj)
						continue
					}
				}
			}
			if(params.selectCalcTypes.includes("*") && obj.firstNum*obj.secondNum<params.resultNum){
				if(obj.firstNum*obj.secondNum!=1){
					obj.calcType="*"
					arr.push(obj)
					continue
				}
			}
			if(params.selectCalcTypes.includes("/") && obj.firstNum/obj.secondNum<params.resultNum){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				if(obj.firstNum / obj.secondNum <params.resultNum){
					if(obj.firstNum/obj.secondNum!=1){
						obj.calcType="/"
						arr.push(obj)
						continue
					}
				}
			}
		}else{
			if(params.selectCalcTypes.includes("+") && obj.firstNum+obj.secondNum<params.resultNum){
				obj.calcType="+"
				arr.push(obj)
				continue
			}
			if(params.selectCalcTypes.includes("-") && obj.firstNum-obj.secondNum<params.resultNum){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				obj.calcType="-"
				arr.push(obj)
				continue
			}
			if(params.selectCalcTypes.includes("*") && obj.firstNum*obj.secondNum<params.resultNum){
				obj.calcType="*"
				arr.push(obj)
				continue
			}
			if(params.selectCalcTypes.includes("/") && obj.firstNum/obj.secondNum<params.resultNum){
				if(obj.firstNum<obj.secondNum){
					let c = obj.secondNum
					obj.secondNum = obj.firstNum
					obj.firstNum = c
				}
				obj.calcType="/"
				arr.push(obj)
				continue
			}
		}
	}
	return arr
}

// 二则运算
export function doubleCalc(length,min,max,ops){
	let arr = []
	while (arr.length < length) {
		let a = getRandomInt(min, max);
		let ab = "";
		let b = getRandomInt(min, max);
		let bc = "";
		let c = getRandomInt(min, max);
		if ((a + b + c) < max) {
			arr.push({ a, b, c, ab:"+", bc:"+" });
			continue
		} else if ((a + b - c) < max && (a+b-c)>0) {
			arr.push({ a, b, c, ab:"+", bc:"-" });
			continue
		} else if ((a - b + c) < max && (a-b+c)>0) {
			arr.push({ a, b, c, ab:"-", bc:"+" });
			continue
		} else if ((a - b - c) < max && (a-b-c)>0) {
			arr.push({ a, b, c, ab:"-", bc:"-" });
			continue
		}else{
			continue
		}
	}
	let brr = arr.map(item=>{
		return {
			firstNum:item.a,
			secondNum:item.b,
			thirdNum:item.c,
			firstCalcType:item.ab,
			secondCalcType:item.bc
		}
	})
	return brr
}

function getRandomInt(min, max) {
	// 获取随机整数
	const minCeil = Math.ceil(min)
	const maxFloor = Math.floor(max)
	return Math.floor(Math.random() * (maxFloor - minCeil + 1) + minCeil)
}

function getCalc(nz, no, max) {
	if (nz && no) {
		return getRandomInt(2, max)
	} else if (nz && !no) {
		return getRandomInt(1, max)
	} else if (!nz && no) {
		let res = getRandomInt(0, max)
		while (res == 1) {
			res = getRandomInt(0, max)
		}
		return res
	} else {
		return getRandomInt(0, max)
	}
}

function calcNum(a,b,t){
	a = Number(a)
	b = Number(b)
	if(t === "+"){
		return a+b
	}
	if(t==="-"){
		return a-b
	}
	if(t==="*"){
		return a*b
	}
	if(t==="/"){
		return a / b
	}
}

export function getDoubleScore(data,id,isEdit){
	let arr = JSON.parse(data.calcList)
	let savearr=[]
	let truecount=0
	let obj = {
		score:100,
		useTimeTxt:data.useTime,
		accuracy:0
	}
	arr.forEach(item=>{
		if(item.myResult != calcNum((calcNum(item.firstNum,item.secondNum,item.firstCalcType)),item.thirdNum,item.secondCalcType)){
			obj.score -= (100 / arr.length)
			item.resultFlag = "error"
		}else{
			item.resultFlag = "success"
			truecount++
		}
		let _obj = {
			topic:`${item.firstNum}${item.firstCalcType}${item.secondNum}${item.secondCalcType}${item.thirdNum}=`,
			userAnswer:item.myResult,
			answer:calcNum((calcNum(item.firstNum,item.secondNum,item.firstCalcType)),item.thirdNum,item.secondCalcType),
			isCorrect:item.resultFlag=='error'?0:1
		}
		console.log(calcNum((calcNum(item.firstNum,item.secondNum,item.firstCalcType)),item.thirdNum,item.secondCalcType),'answer');
		if(isEdit!=0){
			_obj.id = item.id
		}
		savearr.push(_obj)
	})
	obj.accuracy = Number(parseFloat((truecount / arr.length)*100).toFixed(0))
	uni.setStorageSync(String(id),JSON.stringify(arr))
	return [obj,savearr]
}

export function getScore(data,id,isEdit){
	let arr = JSON.parse(data.calcList)
	let savearr=[]
	let truecount=0
	let obj = {
		score:100,
		useTimeTxt:data.useTime,
		accuracy:0
	}
	arr.forEach(item=>{
		if(item.myResult != calcNum(item.firstNum,item.secondNum,item.calcType)){
			obj.score -= (100 / arr.length)
			item.resultFlag = "error"
		}else{
			item.resultFlag = "success"
			truecount++
		}
		let _obj = {
			topic:`${item.firstNum}${item.calcType}${item.secondNum}=`,
			userAnswer:item.myResult,
			answer:calcNum(item.firstNum,item.secondNum,item.calcType),
			isCorrect:item.resultFlag=='error'?0:1
		}
		if(isEdit!=0){
			_obj.id = item.id
		}
		savearr.push(_obj)
	})
	obj.accuracy = Number(parseFloat((truecount / arr.length)*100).toFixed(0))
	uni.setStorageSync(String(id),JSON.stringify(arr))
	return [obj,savearr]
}

export function pinTen(){
	let arr = []
	while(arr.length<10){
		let a = getRandomInt(11,19)
		let b1 = getRandomInt(0,9)
		let b2 = getRandomInt(0,9)
		let c = a-(b1+b2)
		
		if(a-10 ===b1 && 10-b2===c && (b1+b2)>1 && (b1+b2) === a-c){
			arr.push({
				firstNum:a,
				secondNum:b1+b2,
				resultNum:c
			})
		}
	}
	// console.log(arr);
	return arr
}